package com.igeek.ch02.controller;

import com.igeek.ch02.common.CommonResult;
import com.igeek.ch02.entity.User;
import com.igeek.ch02.service.UserService;
import com.igeek.ch02.utils.JSONUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;


@WebServlet(name = "UserViewOneServlet",urlPatterns = "/userViewOne")
public class UserViewOneServlet extends HttpServlet {

    UserService userService=new UserService();

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
        int id=Integer.parseInt(request.getParameter("id"));

        User user =userService.viewOne(id);

        CommonResult commonResult =null;
        if (user!=null){
            commonResult=new CommonResult(200,"查询成功",user);
        }else {
            commonResult=new CommonResult(488,"查询失败");
        }
        JSONUtils.writeJson(response,commonResult);

    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
        this.doPost(request, response);
    }
}
